/*
 * $Id: popen.h,v 1.2 1999/08/02 19:48:07 dms Exp $
 *
 */

#ifndef popen_h
#define popen_h

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/wait.h>

#include <popen/MyException.h>

DECLARE_EXCEPTION(Popen);

class Popen
{
  enum t_cleanup_mode{UNINIT, OPEN, OPEN2, OPENSTDERR} cleanup_mode;
  pid_t _child_pid;
  int   _pipe_fd, _pipe_fd_in, _pipe_fd_out, _pipe_fd_stderr;
  FILE* FH_1, *FH_2;

  int break_up(const char *str, char *argv[], int max_args);
  void clean(char *argv[]);

public:
	Popen():
		cleanup_mode(UNINIT),
		_child_pid(-1),
		_pipe_fd (-1), _pipe_fd_in (-1), _pipe_fd_out (-1), _pipe_fd_stderr (-1),
		FH_1(0), FH_2(0){}
  FILE *open(const char *program, const char *env, const char *type);
  FILE *open(const char *program, const char *type){ return open(program,0,type); }
  FILE *open_stderr(const char *arg_program, FILE **se, const char *arg_env, const char *type);
  FILE *open_stderr(const char *arg_program, FILE **se, const char *type)
  {
	  return open_stderr(arg_program, se, 0, type);
  }


  void open2(const char *program, const char *env, FILE **sf, FILE **df);
  void open2(const char *program, FILE **sf, FILE **df){ open2(program,0,sf,df); }

  int close();
  int close(FILE *iop);
  int close2(FILE *sf, FILE *df);
  int close_stderr (FILE *iop, FILE *se);
};

#endif
